package com.robusta.cityuate.objects;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.robusta.cityuate.services.DeleteOfferingService;
import com.robusta.cityuate.services.HTTPService;
import com.robusta.cityuate.utils.ConnectionUtils;
import com.robusta.cityuate.utils.TimeUtils;

@SuppressWarnings("serial")
public class Offering implements Serializable {

	public static final String OFFERING_START_TIME_1I = "start_time(1i)";
	public static final String OFFERING_START_TIME_2I = "start_time(2i)";
	public static final String OFFERING_START_TIME_3I = "start_time(3i)";
	public static final String OFFERING_START_TIME_4I = "start_time(4i)";
	public static final String OFFERING_START_TIME_5I = "start_time(5i)";
	public static final String OFFERING_END_TIME_1I = "end_time(1i)";
	public static final String OFFERING_END_TIME_2I = "end_time(2i)";
	public static final String OFFERING_END_TIME_3I = "end_time(3i)";
	public static final String OFFERING_END_TIME_4I = "end_time(4i)";
	public static final String OFFERING_END_TIME_5I = "end_time(5i)";
	public static final String OFFERING_EXPIRY_1I = "expiry_date(1i)";
	public static final String OFFERING_EXPIRY_2I = "expiry_date(2i)";
	public static final String OFFERING_EXPIRY_3I = "expiry_date(3i)";
	public static final String OFFERING_STATUS = "status";
	
	

	public final static String OFFERING = "offering";
	public static final String OFFERING_NAME = "name";
	public static final String OFFERING_RATE = "rate";
	public static final String OFFERING_MIN_RATING = "minimum_rating";
	public static final String OFFERING_RECURRING = "recurring";
	public static final String OFFERING_START_TIME = "start_time";
	public static final String OFFERING_END_TIME = "end_time";
	public static final String OFFERING_TIME_SLOTS = "time_slots";
	public static final String OFFERING_EXPIRY_DATE = "expiry_date";
	public static final String OFFERING_ID = "id";
	static final String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssz";
	private static final String DATE_FORMAT = "yyyy-MM-dd";

	/*
	 * {"offering":{"name":"test","start_time":"2012-12-04T15:52:00+01:00",
	 * "end_time"
	 * :"2012-12-04T16:22:00+01:00","rate":22334.0,"recurring":"w","minimum_rating"
	 * :
	 * 2323.0,"expiry_date":"2012-12-11","time_slots":[{"time_slot":{"start_time"
	 * :"2012-12-04T15:52:00+01:00","end_time":"2012-12-04T16:22:00+01:00",
	 * "time_slots"
	 * :[]}},{"time_slot":{"start_time":"2012-12-11T15:52:00+01:00","end_time"
	 * :"2012-12-11T16:22:00+01:00","time_slots":[]}}]}}
	 */
	Spot spot;
	String name;
	// w for weekly , d for daily , "" for nonrecurring
	String recurring;
	long id;
	double creditRate;
	double minRating;
	Date startTime;
	Date endTime;
	Date expiryDate;
	ArrayList<TimeSlot> timeSlots = new ArrayList<TimeSlot>();
	private String status="free";
	

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public Offering(Spot spot) {
		this.spot = spot;
	}

	public Spot getSpot() {
		return spot;
	}

	public void setSpot(Spot spot) {
		this.spot = spot;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getRecurring() {
		return recurring;
	}

	public void setRecurring(String recurring) {
		this.recurring = recurring;
	}

	public double getCreditRate() {
		return creditRate;
	}

	public void setCreditRate(double creditRate) {
		this.creditRate = creditRate;
	}

	public double getMinRating() {
		return minRating;
	}

	public void setMinRating(double minRating) {
		this.minRating = minRating;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public Date getExpiryDate() {
		return expiryDate;
	}

	public void setExpiryDate(Date expiryDate) {
		this.expiryDate = expiryDate;
	}

	public ArrayList<TimeSlot> getTimeSlots() {
		return timeSlots;
	}

	public void setTimeSlots(ArrayList<TimeSlot> timeSlots) {
		this.timeSlots = timeSlots;
	}

	public static Offering getOfferingFromJSON(JSONObject jsonObject, Spot spot) {
		Offering offering = new Offering(spot);

		try {
			// JSONObject offerObj = new JSONObject(jsonObject);
			JSONObject data = jsonObject.getJSONObject(OFFERING);
			//System.out.println("offering "+data.toString());
			offering.name = data.getString(OFFERING_NAME);
			offering.creditRate = data.getDouble(OFFERING_RATE);
			if (!data.isNull(OFFERING_MIN_RATING))
				offering.minRating = data.getDouble(OFFERING_MIN_RATING);
			if (!data.isNull(OFFERING_RECURRING))
				offering.recurring = data.getString(OFFERING_RECURRING);
			if (!data.isNull(OFFERING_STATUS))
				offering.status = data.getString(OFFERING_STATUS);
			offering.startTime = TimeUtils.getDateTimeFromString(
					data.getString(OFFERING_START_TIME), DATE_TIME_FORMAT);
			offering.endTime = TimeUtils.getDateTimeFromString(
					data.getString(OFFERING_END_TIME), DATE_TIME_FORMAT);
			if (!data.isNull(OFFERING_EXPIRY_DATE))
				offering.expiryDate = TimeUtils.getDateTimeFromString(
						data.getString(OFFERING_EXPIRY_DATE), DATE_FORMAT);
			offering.id = data.getLong(OFFERING_ID);
			// load time slots
			JSONArray timeSlotsJSONArrays = data
					.getJSONArray(OFFERING_TIME_SLOTS);
			for (int i = 0; i < timeSlotsJSONArrays.length(); i++)
				{
				TimeSlot timeSlot= TimeSlot
						.getTimeSlotfromJSON(timeSlotsJSONArrays
								.getJSONObject(i),offering);
				if(timeSlot.isReserved()&&!spot.isCreator())
					continue;
				else
					offering.timeSlots.add(timeSlot);
				}
			return offering;

		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}



	
	public void delete(Context mContext,String tag) {
		if (!ConnectionUtils.isNetworkAvailable(mContext))// {
			ConnectionUtils.showNoConnectionDialog(mContext);

		Intent backgroud_regService = new Intent(mContext,
				DeleteOfferingService.class);
		backgroud_regService.setAction(tag);

		backgroud_regService.putExtra(HTTPService.LOGIN_RQUIRED, true);
		backgroud_regService.putExtra(OFFERING, this);
		mContext.startService(backgroud_regService);
		Log.e(tag, "Fired DeleteOffering service");

	}
	
	public boolean hasReservation(){
		for (TimeSlot ts : timeSlots) {
			if(ts.isReserved())
				return true;
		}
	return false;	
	}

}
